package com.brain.activity;

import com.brain.util.DBSettingData;
import com.brain.vo.SettingVO;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

public class HealingGameActivity extends Activity implements View.OnTouchListener {
	private ImageButton  play_btn;
	private ImageView    tree_img, level_img, level_up_img;
	private int   level_res[] = {0,
			                     R.drawable.back_8_1,
			                     R.drawable.back_8_2,
			                     R.drawable.back_8_3,
			                     R.drawable.back_8_4,
			                     R.drawable.back_8_5,
			                     R.drawable.back_8_6,
			                     R.drawable.back_8_7,
			                     R.drawable.back_8_8,
			                     R.drawable.back_8_9,
			                     R.drawable.back_8_10};
	private Handler mHandler = new Handler();
	private Handler mHandler1 = new Handler();
	private Handler mHandler2 = new Handler();
	private int imageIndex = 0;
	private int levelIndex = 0;
	private int preLevelChk = 0;
	private int levelChk[] = {2,4,7,10};
	private int x = 0;
	boolean bCheck = false;
    DBSettingData   db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.healing_game);
        db = new DBSettingData(this);
        SettingVO vo = db.getData(this);
        if(vo != null) {
        	if(vo.getTutorialChk().equals("Y")) {
        	} else {
        		Intent intent = new Intent(this, TutorialActivity.class);
        		intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        		startActivity(intent);		   
        	}
        } else {
    		Intent intent = new Intent(this, TutorialActivity.class);
    		intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    		startActivity(intent);		   
        }        
		
        play_btn 			= (ImageButton) 	findViewById(R.id.play_btn);
        tree_img 			= (ImageView)   	findViewById(R.id.tree_img);
        
        play_btn.setOnTouchListener(HealingGameActivity.this);

   }
    
    
    public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		 if(v == play_btn) {  
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				tree_img.setImageResource(R.drawable.image8_c);
				play_btn.setImageResource(R.drawable.play_button_c);

		        imageIndex = 0;
//				setBluetooth();
				setLevelScreen();
			}
		}
		return false;
	}
    

    private void setLevelScreen() {
 
        level_up_img 		= (ImageView)   	findViewById(R.id.level_up_img);
        level_img 			= (ImageView)   	findViewById(R.id.level_img);
        level_img.setVisibility(View.VISIBLE);
        
        levelIndex = loopLevel();
        
		mHandler.removeCallbacks(mUpdateTimeTask);

	    mHandler1.removeCallbacks(mLevelUpTimeTask);
	    mHandler.postDelayed(mUpdateTimeTask, 3000);
	   
   }

    private int loopLevel() {
		System.out.println("aaaaaaaa " + x);
		int level = 0;
		if(x < 4) {
			level = levelChk[x];
			x++;
		} else {
			level = -1;
		}
    	return level;
    }
    
	
    private void setSucessScreen() {
		level_img.setBackgroundResource(R.drawable.background_sucess);
		level_up_img.setVisibility(View.GONE);   	
    }
    
    private void setFailScreen() {
		level_img.setBackgroundResource(R.drawable.background_fail);
		level_up_img.setVisibility(View.GONE);   
    }

    
	private Runnable mUpdateTimeTask = new Runnable() 
	{   
		@Override
		public void run() 
		{ 
	
  		  rotateImg(imageIndex++);
		  if(imageIndex > levelIndex) {
//			  imageIndex = 0;
			  level_up_img.setVisibility(View.GONE);
		      
//			  mHandler.sendEmptyMessage(0);
//			  mHandler.postDelayed(mUpdateTimeTask, 3000);
			  levelIndex = loopLevel();
			  System.out.println("levelIndex == " +  levelIndex);
			  
		      if(imageIndex == 10) {
		    	  imageIndex = 100;
		      } else if(levelIndex == -1) {
				  mHandler.sendEmptyMessage(0);
			  } else if(x > 0 && levelIndex < preLevelChk) {
				  mHandler.postDelayed(mUpdateTimeTask, 3000);
				  setFailScreen();
				  mHandler.sendEmptyMessage(0);
			  } else {
			   	  System.out.println("5");
				  mHandler1.postDelayed(mLevelUpTimeTask,1000 );
				  mHandler.postDelayed(mUpdateTimeTask, 3000);
			  }

		  } else if(bCheck == true) {
			  imageIndex = 100;
		  }	else {
			  mHandler.postDelayed(mUpdateTimeTask, 3000);
			  
			  if(imageIndex > 1) {
				  mHandler1.postDelayed(mLevelUpTimeTask,1000 );
//				  level_up_img.setVisibility(View.VISIBLE);
			  }
	   	  }
		  preLevelChk = levelIndex;  
		}
	};
	
	private Runnable mLevelUpTimeTask = new Runnable() 
	{   
		@Override
		public void run() 
		{   
			  level_up_img.setVisibility(View.VISIBLE);
			  mHandler1.sendEmptyMessage(0);
		}
	};
    
	private void rotateImg(int i)
	{	        
		System.out.println("i = " + i + " levelIndex = "+ levelIndex);
		if(i <= 10) {
			level_img.setBackgroundResource(level_res[i++]);
			level_up_img.setVisibility(View.GONE);
			if(i == 10) {
				bCheck = true;
			}
		} else if(i >= 100) {
	    	setSucessScreen();
		}
	}   
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
    }	     
}
